﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace syc.Fc
{
    public partial class 保护器库存管理 : System.Web.UI.Page
    {
        protected string Notice = "";
        string sql =
@"SELECT t1.ID
,case when t1.套数>0 then '判定入库' else '转移至'+cast(生产单位ID as varchar)+'分厂' end as 入出
,case when t1.产品类别ID=1 then '油管螺纹保护器 ' else '套管螺纹保护器 ' end as 品名
,t2.名称 as 铁质,t3.名称 as 塑质,t1.套数,t1.件数,t1.日期,t1.合格,t1.制票
FROM 自产保护器判定单 t1
left outer join 原料供货单位 t2 on t2.ID=t1.铁质供货商ID
left outer join 材料供货单位 t3 on t3.ID=t1.塑质供货商ID";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.TieZhi.DataBind();
                this.SuZhi.DataBind();
                this.Button_Query_Click(null, null);
            }
            else
            {
                this.SqlDataSource1.SelectCommand = this.SqlText.Text;
            }
        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.GridView1.SelectedIndex = -1;
            this.Button_View.Enabled = false;
            this.Button_Edit.Enabled = false;
            this.Button_Delete.Enabled = false;

            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }
        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            Label_Total.Text = "共查询到 " + e.AffectedRows + " 项记录";
        }
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

            this.Button_Edit.Enabled = false;
            this.Button_Delete.Enabled = false;
            this.Button_View.Enabled = false;

            if ((int)this.GridView1.SelectedDataKey[1] > 0)
            {
                this.Button_View.Enabled = true;
                this.Button_View.OnClientClick = "parent.swin2_load('/Fc/保护器入库单.aspx?id="
                    + this.GridView1.SelectedDataKey[0].ToString() + "',700,430);";
                if (!(bool)this.GridView1.SelectedDataKey[2])
                {
                    this.Button_Edit.Enabled = true;
                    this.Button_Delete.Enabled = true;
                    this.Button_Edit.OnClientClick = "parent.swin2_load('/Fc/保护器入库单录入.aspx?id="
                        + this.GridView1.SelectedDataKey[0].ToString() + "',560,320);";
                }

            }
            else
            {
                this.Button_Edit.Enabled = true;
                this.Button_Delete.Enabled = true;
                this.Button_Edit.OnClientClick = "parent.swin2_load('/Fc/保护器出库单录入.aspx?id="
                    + this.GridView1.SelectedDataKey[0].ToString() + "',560,256);";
            }
            
        }
        private string GetState()
        {
            string state = "";

            if (this.FenChang.Text != "0")
            {
                state += " and t1.生产单位ID =" + this.FenChang.Text;
            }
            if (this.TieZhi.Text != "0")
            {
                state += " and t1.铁质供货商ID = " + this.TieZhi.Text;
            }
            if (this.SuZhi.Text != "0")
            {
                state += " and t1.塑质供货商ID = " + this.SuZhi.Text;
            }
            if (this.PanDing.Text != "-1")
            {
                state += " and t1.合格 = " + this.PanDing.Text;
            }
            DateTime d;
            if (DateTime.TryParse(this.Date1.Text, out d))
            {
                state += " and t1.日期>='" + d.ToShortDateString() + "'";
            }
            if (DateTime.TryParse(this.Date2.Text, out d))
            {
                state += " and t1.日期<='" + d.ToShortDateString() + "'";
            }

            if (state.StartsWith(" and"))
            {
                state = state.Substring(4);
            }
            if (state != "")
            {
                state = " where " + state;
            }
            return state;
        }
        protected void Button_Query_Click(object sender, EventArgs e)
        {
            string state = this.GetState();
            this.SqlText.Text = sql + state + " order by ID desc";
            this.SqlDataSource1.SelectCommand = "";
            this.SqlDataSource1.SelectCommand = this.SqlText.Text;
        }

        protected void Button_Reset_Click(object sender, EventArgs e)
        {
            this.FenChang.Text = "0";
            this.TieZhi.Text = "0";
            this.SuZhi.Text = "0";

            this.Date1.Text = "";
            this.Date2.Text = "";

            this.PanDing.Text = "-1";
        }

        protected void Button_Delete_Click(object sender, EventArgs e)
        {
            this.SqlDataSource1.DeleteParameters["ID"].DefaultValue = this.GridView1.SelectedDataKey[0].ToString();
            if (this.SqlDataSource1.Delete() > 0)
            {
                this.Notice = "·删除成功";
            }
            else
            {
                this.Notice = "·删除失败";
            }
        }
    }
}
